![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
babel-plugin-lodash
Advanced tools
babel-plugin-lodash is a Babel plugin that cherry-picks Lodash modules so you only include the parts of Lodash you use, reducing bundle size and improving performance.
Cherry-picking Lodash modules
This feature allows you to import only the Lodash functions you need, rather than the entire library. This reduces the size of your JavaScript bundle.
import { map, filter } from 'lodash';
Automatic cherry-picking
Even if you import the entire Lodash library, babel-plugin-lodash will automatically transform your code to only include the specific functions you use.
import _ from 'lodash';
const result = _.map([1, 2, 3], n => n * 2);
lodash-es is a version of Lodash that is designed to work with ES modules. It allows for tree-shaking, which can help reduce bundle size. Unlike babel-plugin-lodash, lodash-es requires you to change your import statements to use ES module syntax.
lodash-webpack-plugin is a plugin for Webpack that also helps reduce the size of Lodash in your bundle. It works by stripping out unused features of Lodash, but it requires a Webpack-specific setup, unlike babel-plugin-lodash which works directly with Babel.
A simple transform to cherry-pick Lodash modules so you don’t have to.
Combine with lodash-webpack-plugin for even smaller cherry-picked builds!
$ npm i --save lodash
$ npm i --save-dev babel-plugin-lodash @babel/cli @babel/preset-env
Transforms
import _ from 'lodash'
import { add } from 'lodash/fp'
const addOne = add(1)
_.map([1, 2, 3], addOne)
roughly to
import _add from 'lodash/fp/add'
import _map from 'lodash/map'
const addOne = _add(1)
_map([1, 2, 3], addOne)
{
"plugins": ["lodash"],
"presets": [["@babel/env", { "targets": { "node": 6 } }]]
}
Set plugin options using an array of [pluginName, optionsObject]
.
{
"plugins": [["lodash", { "id": "lodash-compat", "cwd": "some/path" }]],
"presets": [["@babel/env", { "targets": { "node": 6 } }]]
}
The options.id
can be an array of ids.
{
"plugins": [["lodash", { "id": ["async", "lodash-bound"] }]],
"presets": [["@babel/env", { "targets": { "node": 6 } }]]
}
$ babel --plugins lodash --presets @babel/es2015 script.js
require('babel-core').transform('code', {
'plugins': ['lodash'],
'presets': [['@babel/env', { 'targets': { 'node': 6 } }]]
})
'module': {
'loaders': [{
'loader': 'babel-loader',
'test': /\.js$/,
'exclude': /node_modules/,
'query': {
'plugins': ['lodash'],
'presets': [['@babel/env', { 'targets': { 'node': 6 } }]]
}
}]
}
Can this plugin produce ES2015 imports rather than CommonJS imports?
This plugin produces ES2015 imports by default. The
@babel/plugin-transform-modules-commonjs
plugin, which is included in the @babel/preset-es2015
preset, transforms ES2015 import
statements to CommonJS. Omit it from your
preset to preserve ES2015 style imports.
FAQs
Modular Lodash builds without the hassle.
The npm package babel-plugin-lodash receives a total of 658,940 weekly downloads. As such, babel-plugin-lodash popularity was classified as popular.
We found that babel-plugin-lodash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.